//
//  CustomerUIPickerView.m
//  at_xiaoge_client
//
//  Created by Linyu Niu on 27/03/2018.
//  Copyright © 2018 Linyu Niu. All rights reserved.
//

#import "CustomerUIPickerView.h"

@interface CustomerUIPickerView(){
    UIPickerView * pickerView;
    UIView * pickerBgView;
}
@end


@implementation CustomerUIPickerView

@synthesize params;

-(instancetype)initWithTargetView:(UIView *)view delegate:(id<UIPickerViewDelegate,UIPickerViewDataSource>)delegate{
    self = [super initWithFrame:view.bounds];
    self.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
    self.alpha = 0;
    [view addSubview:self];
    
    [self setupPickerView:view delegate:delegate];
    return self;
}


-(void)setupPickerView:(UIView *)targetView delegate:(id<UIPickerViewDelegate,UIPickerViewDataSource>)delegate{
    pickerBgView = [[UIView alloc] init];
    pickerBgView.tag = 180326;
    pickerBgView.frame = CGRectMake(0, targetView.frame.size.height, targetView.frame.size.width, 250);
    pickerBgView.backgroundColor = [UIColor whiteColor];
    
    [targetView addSubview:pickerBgView];
    
    UIButton * cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelButton.frame = CGRectMake(15, 20, 50, 20);
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    cancelButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [cancelButton setTitleColor:UIColorFromRGB(0x999999) forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(remove) forControlEvents:UIControlEventTouchUpInside];
    [pickerBgView addSubview:cancelButton];
    
    
    UIButton * confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmButton.frame = CGRectMake(pickerBgView.frame.size.width - 15 - 50, cancelButton.frame.origin.y,cancelButton.frame.size.width, cancelButton.frame.size.height);
    confirmButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
    confirmButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [confirmButton addTarget:self action:@selector(confirm) forControlEvents:UIControlEventTouchUpInside];
    [confirmButton setTitleColor:UIColorFromRGB(0x20C67A) forState:UIControlStateNormal];
    [pickerBgView addSubview:confirmButton];
    
    UIImageView * lineView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Line"]];
    lineView.frame = CGRectMake(0, confirmButton.frame.origin.y + confirmButton.frame.size.height + 15, pickerBgView.frame.size.width, 1.0);
    [pickerBgView addSubview:lineView];
    
    pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, lineView.frame.origin.y + lineView.frame.size.height, pickerBgView.frame.size.width, pickerBgView.frame.size.height - lineView.frame.origin.y - lineView.frame.size.height)];
    pickerView.delegate = delegate;
    pickerView.dataSource = delegate;
    [pickerView reloadAllComponents];
    [pickerBgView addSubview:pickerView];
}
-(void)confirm
{
    void(^block)(void) = [params objectForKey:@"action"];
    if (block) {
        block();
    }
    [self remove];
}

-(void)remove{
    
    CGRect frame = pickerBgView.frame;
    frame.origin.y = [UIScreen mainScreen].bounds.size.height;
    
    
    [UIView animateWithDuration:0.35 animations:^(void){
        pickerBgView.frame = frame;
        self.alpha = 0;
    } completion:^(BOOL finish){
        if (finish) {
            [pickerBgView removeFromSuperview];
            [self removeFromSuperview];
        }
    }];
}

-(void)show{
    
    NSLog(@"pickerBgView------>%@",NSStringFromCGRect(pickerView.frame));
    CGRect frame = pickerBgView.frame;
    frame.origin.y = pickerBgView.superview.frame.size.height - pickerBgView.frame.size.height;
    
    [UIView animateWithDuration:0.35 animations:^(void){
        pickerBgView.frame = frame;
        self.alpha = 1.0;
    }];
}

-(void)dealloc{
    NSLog(@"%s",__PRETTY_FUNCTION__);
}
@end

